{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "## Python 案例练习\n",
    "\n",
    "学习任何计算机编程语言，案例和案例的练习可以激发学生的学习兴趣，使得Python这门编程语言的学习变得更加有趣。"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 简单数据类型\n",
    "\n",
    "所谓\"简单数据类型\"是针对Python这门课程而言的，在具体一点就是我的这门课是这么划分的，其他的Python课程可能有不同的划分类型。在这里，简单数据类包括：\n",
    "\n",
    "* 数值类型\n",
    "  * 整数，int\n",
    "  * 浮点数，float\n",
    "* 字符串类型，str\n",
    "* 布尔类型，bool，布尔类型仅包括两个具体的值：\n",
    "  * True，真\n",
    "  * False，假"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10 <class 'int'>\n",
      "10.23 <class 'float'>\n",
      "Hello, World! <class 'str'>\n",
      "True <class 'bool'>\n",
      "False <class 'bool'>\n"
     ]
    }
   ],
   "source": [
    "# 数值类型\n",
    "d = 10\n",
    "print(d, type(d))\n",
    "\n",
    "d = 10.23\n",
    "print(d, type(d))\n",
    "\n",
    "s = \"Hello, World!\"\n",
    "print(s, type(s))\n",
    "\n",
    "b1 = True\n",
    "print(b1, type(b1))\n",
    "\n",
    "b2 = False\n",
    "print(b2, type(b2))"
   ]
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 对象数据类型\n",
    "\n",
    "本课程所说的对象数据类型指：列表（list，数组）、字典（dict，键值对，json）、集合（set，无序、不重复）、元组（tuple，与列表相似，不可修改）、日期类型\n",
    "\n",
    "### 列表"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[] <class 'list'>\n",
      "[1, 2, 3, 4, 5] <class 'list'>\n"
     ]
    }
   ],
   "source": [
    "# 空列表初始化\n",
    "lst = []\n",
    "print(lst, type(lst))\n",
    "\n",
    "# 列表初始化\n",
    "lst = [1, 2, 3, 4, 5]\n",
    "print(lst, type(lst))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4] <class 'list'>\n"
     ]
    }
   ],
   "source": [
    "# 使用加号（+）对列表进行运算\n",
    "l1 = [1, 2]\n",
    "l2 = [3, 4]\n",
    "lst = l1 + l2\n",
    "print(lst, type(lst))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 'X', 3, 4] <class 'list'>\n",
      "[1, 'Z', 2, 'X', 3, 4] <class 'list'>\n",
      "[1, 'Z', 2, 'X', 3, 'T', 4] <class 'list'>\n"
     ]
    }
   ],
   "source": [
    "# 使用insert方法向列表中添加元素\n",
    "lst = [1, 2, 3, 4]\n",
    "\n",
    "lst.insert(2, 'X')\n",
    "print(lst, type(lst))\n",
    "\n",
    "lst.insert(1, 'Z')\n",
    "print(lst, type(lst))\n",
    "\n",
    "lst.insert(5, 'T')\n",
    "print(lst, type(lst))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 5] <class 'list'>\n",
      "[1, 2, 3, 4, 5, 6] <class 'list'>\n"
     ]
    }
   ],
   "source": [
    "# 使用append向列表添加元素\n",
    "lst = [1, 2, 3, 4]\n",
    "\n",
    "lst.append(5)\n",
    "print(lst, type(lst))\n",
    "\n",
    "lst.append(6)\n",
    "print(lst, type(lst))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, [3, 4]] <class 'list'>\n",
      "[1, 2, 3, 4, [3, 4], [5, 6]] <class 'list'>\n"
     ]
    }
   ],
   "source": [
    "# 使用append向列表中添加列表\n",
    "lst = [1, 2, 3, 4]\n",
    "\n",
    "l1 = [3, 4]\n",
    "lst.append(l1)\n",
    "print(lst, type(lst))\n",
    "\n",
    "l2 = [5, 6]\n",
    "lst.append(l2)\n",
    "print(lst, type(lst))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 'a', 'b'] <class 'list'>\n",
      "[1, 2, 3, 4, 'a', 'b', 'c', 'd'] <class 'list'>\n"
     ]
    }
   ],
   "source": [
    "# 使用extend向列表添加元素\n",
    "lst = [1, 2, 3, 4]\n",
    "\n",
    "l1 = ['a', 'b']\n",
    "lst.extend(l1)\n",
    "print(lst, type(lst))\n",
    "\n",
    "l2 = ['c', 'd']\n",
    "lst.extend(l2)\n",
    "print(lst, type(lst))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 'a', 'b', 'c'] <class 'list'>\n",
      "[1, 2, 3, 4, 'a', 'b'] 返回结果： c\n",
      "[1, 2, 4, 'a', 'b'] 返回结果： 3\n"
     ]
    }
   ],
   "source": [
    "# 使用pop方法从列表中弹出元素\n",
    "lst = [1, 2, 3, 4, 'a', 'b', 'c']\n",
    "\n",
    "print(lst, type(lst))\n",
    "\n",
    "rst = lst.pop()\n",
    "print(lst, \"返回结果：\", rst)\n",
    "\n",
    "# 弹出指定位置的元素\n",
    "rst = lst.pop(2)\n",
    "print(lst, \"返回结果：\", rst)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 'a', 'b', 'c', 'a', 'b', 4] <class 'list'>\n",
      "[1, 2, 3, 4, 'b', 'c', 'a', 'b', 4] <class 'list'>\n",
      "[1, 2, 3, 'b', 'c', 'a', 'b', 4] <class 'list'>\n"
     ]
    }
   ],
   "source": [
    "# 使用remove删除列表中的某个元素\n",
    "# 列表中第一个匹配的元素将被删除\n",
    "lst = [1, 2, 3, 4, 'a', 'b', 'c', 'a', 'b', 4]\n",
    "\n",
    "print(lst, type(lst))\n",
    "\n",
    "lst.remove('a')\n",
    "print(lst, type(lst))\n",
    "\n",
    "lst.remove(4)\n",
    "print(lst, type(lst))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 'a', 'b', 'c'] <class 'list'>\n",
      "3\n",
      "a\n"
     ]
    }
   ],
   "source": [
    "# 使用下标访问列表元素\n",
    "lst = [1, 2, 3, 4, 'a', 'b', 'c']\n",
    "\n",
    "print(lst, type(lst))\n",
    "\n",
    "print(lst[2])\n",
    "print(lst[4])"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 'a', 'b', 'c'] <class 'list'>\n",
      "[1, 2, 'Hello', 4, 'a', 'ZT', 'c'] <class 'list'>\n"
     ]
    }
   ],
   "source": [
    "# 使用下标修改列表元素\n",
    "lst = [1, 2, 3, 4, 'a', 'b', 'c']\n",
    "\n",
    "print(lst, type(lst))\n",
    "\n",
    "lst[2] = 'Hello'\n",
    "lst[5] = 'ZT'\n",
    "\n",
    "print(lst, type(lst))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 'a', 'b', 'c'] <class 'list'>\n",
      "[1, 2, 3, 'a', 'b', 'c'] <class 'list'>\n",
      "[1, 2, 3, 'a', 'c'] <class 'list'>\n"
     ]
    }
   ],
   "source": [
    "# 使用del删除列表中的某个元素\n",
    "lst = [1, 2, 3, 4, 'a', 'b', 'c']\n",
    "\n",
    "print(lst, type(lst))\n",
    "\n",
    "del lst[3]\n",
    "print(lst, type(lst))\n",
    "\n",
    "del lst[4]\n",
    "print(lst, type(lst))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 'a', 'b', 'c'] <class 'list'>\n",
      "the index position of value 'a' in the lst is 4.\n",
      "the index position of value 3 in the lst is 2.\n"
     ]
    },
    {
     "ename": "ValueError",
     "evalue": "'a' is not in list",
     "output_type": "error",
     "traceback": [
      "\u001B[0;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[0;31mValueError\u001B[0m                                Traceback (most recent call last)",
      "\u001B[0;32m/var/folders/mr/zthpdwtn37g2h4t97k6tv1pw0000gn/T/ipykernel_2308/3369262179.py\u001B[0m in \u001B[0;36m<module>\u001B[0;34m\u001B[0m\n\u001B[1;32m      8\u001B[0m \u001B[0;34m\u001B[0m\u001B[0m\n\u001B[1;32m      9\u001B[0m \u001B[0;31m# 指定索引位置的范围进行查找\u001B[0m\u001B[0;34m\u001B[0m\u001B[0;34m\u001B[0m\u001B[0m\n\u001B[0;32m---> 10\u001B[0;31m \u001B[0mprint\u001B[0m\u001B[0;34m(\u001B[0m\u001B[0;34mf\"the index position of value 'a' in the lst is {lst.index('a', 0, 3)}.\"\u001B[0m\u001B[0;34m)\u001B[0m\u001B[0;34m\u001B[0m\u001B[0;34m\u001B[0m\u001B[0m\n\u001B[0m",
      "\u001B[0;31mValueError\u001B[0m: 'a' is not in list"
     ]
    }
   ],
   "source": [
    "# 使用index返回某个元素的索引位置\n",
    "lst = [1, 2, 3, 4, 'a', 'b', 'c']\n",
    "\n",
    "print(lst, type(lst))\n",
    "\n",
    "print(f\"the index position of value 'a' in the lst is {lst.index('a')}.\")\n",
    "print(f\"the index position of value 3 in the lst is {lst.index(3)}.\")\n",
    "\n",
    "# 指定索引位置的范围进行查找\n",
    "print(f\"the index position of value 'a' in the lst is {lst.index('a', 0, 3)}.\")"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 'a', 'b', 'c'] <class 'list'>\n",
      "列表中包含7个元素\n"
     ]
    }
   ],
   "source": [
    "# 获取列表中的元素个数\n",
    "lst = [1, 2, 3, 4, 'a', 'b', 'c']\n",
    "\n",
    "print(lst, type(lst))\n",
    "\n",
    "print(f\"列表中包含{len(lst)}个元素\")"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 'a', 4, 'a', '4', 'c'] <class 'list'>\n",
      "列表中包含2个'a'\n",
      "列表中包含1个4\n"
     ]
    }
   ],
   "source": [
    "# 获取列表中某个元素的个数\n",
    "lst = [1, 2, 'a', 4, 'a', '4', 'c']\n",
    "\n",
    "print(lst, type(lst))\n",
    "\n",
    "print(f\"列表中包含{lst.count('a')}个'a'\")\n",
    "print(f\"列表中包含{lst.count('4')}个4\")"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[7, 2, 9, 1, 3] <class 'list'>\n",
      "[1, 2, 3, 7, 9] <class 'list'>\n",
      "[9, 7, 3, 2, 1] <class 'list'>\n"
     ]
    }
   ],
   "source": [
    "# 列表排序\n",
    "lst = [7, 2, 9, 1, 3]\n",
    "\n",
    "print(lst, type(lst))\n",
    "\n",
    "# 正序\n",
    "lst.sort()\n",
    "print(lst, type(lst))\n",
    "\n",
    "# 倒序\n",
    "lst.sort(reverse=True)\n",
    "print(lst, type(lst))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[7, 2, 9, 1, 3] <class 'list'>\n",
      "[3, 1, 9, 2, 7] <class 'list'>\n"
     ]
    }
   ],
   "source": [
    "# 列表反转\n",
    "lst = [7, 2, 9, 1, 3]\n",
    "\n",
    "print(lst, type(lst))\n",
    "\n",
    "lst.reverse()\n",
    "print(lst, type(lst))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[7, 2, 9, 1, 3] <class 'list'>\n",
      "9 is in lst => True\n",
      "11 is in lst => False\n",
      "9 is not in lst => False\n",
      "11 is not in lst => True\n"
     ]
    }
   ],
   "source": [
    "# 成员关系\n",
    "lst = [7, 2, 9, 1, 3]\n",
    "\n",
    "print(lst, type(lst))\n",
    "\n",
    "# in\n",
    "print(f\"9 is in lst => {9 in lst}\")\n",
    "print(f\"11 is in lst => {11 in lst}\")\n",
    "\n",
    "# not in\n",
    "print(f\"9 is not in lst => {9 not in lst}\")\n",
    "print(f\"11 is not in lst => {11 not in lst}\")"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 字典"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{} <class 'dict'>\n",
      "{'id': 1, 'name': 'Tome'} <class 'dict'>\n",
      "{'id': None, 'name': None, 'gender': None, 'salary': None} <class 'dict'>\n",
      "{'id': 'v1', 'name': 'v1', 'gender': 'v1', 'salary': 'v1'} <class 'dict'>\n",
      "{'id': 1, 'name': 'Tom', 'gender': 'male', 'salary': 1000} <class 'dict'>\n"
     ]
    }
   ],
   "source": [
    "# 初始化空字典\n",
    "d1 = {}\n",
    "print(d1, type(d1))\n",
    "\n",
    "# 使用下标方式创建字典\n",
    "d1[\"id\"] = 1\n",
    "d1[\"name\"] = \"Tome\"\n",
    "print(d1, type(d1))\n",
    "\n",
    "# 从列表创建字典\n",
    "lst = [\"id\", \"name\", \"gender\", \"salary\"]\n",
    "d1 = {}.fromkeys(lst)\n",
    "print(d1, type(d1))\n",
    "\n",
    "# 从列表创建字典，并指定一个缺省的属性值\n",
    "lst = [\"id\", \"name\", \"gender\", \"salary\"]\n",
    "d1 = {}.fromkeys(lst, \"v1\")\n",
    "print(d1, type(d1))\n",
    "\n",
    "# 初始化字典\n",
    "d1 = {\n",
    "    \"id\": 1,\n",
    "    \"name\": \"Tom\",\n",
    "    \"gender\": \"male\",\n",
    "    \"salary\": 1000\n",
    "}\n",
    "print(d1, type(d1))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'id': 1, 'name': 'Tom', 'gender': 'male', 'salary': 2000, 'extraHours': 8, 'hourlyRate': 30} <class 'dict'>\n",
      "{'id': 1, 'name': 'Tom', 'salary': 2000, 'extraHours': 8, 'hourlyRate': 30} <class 'dict'>\n",
      "{'id': 1, 'name': 'Tom', 'salary': 2000, 'extraHours': 8} <class 'dict'>\n",
      "{'id': 1, 'name': 'Tom', 'salary': 2000} <class 'dict'>\n",
      "{'id': 1, 'name': 'Tom'} <class 'dict'>\n"
     ]
    }
   ],
   "source": [
    "# 从字典中删除键及其键值\n",
    "emp1 = {\n",
    "    \"id\": 1,\n",
    "    \"name\": \"Tom\",\n",
    "    \"gender\": \"male\",\n",
    "    \"salary\": 2000,\n",
    "    \"extraHours\": 8,\n",
    "    \"hourlyRate\": 30\n",
    "}\n",
    "print(emp1, type(emp1))\n",
    "\n",
    "# 使用pop方法删除指定的属性\n",
    "emp1.pop(\"gender\")\n",
    "print(emp1, type(emp1))\n",
    "\n",
    "# 使用pop方法删除指定的属性\n",
    "emp1.pop(\"hourlyRate\")\n",
    "print(emp1, type(emp1))\n",
    "\n",
    "# 使用popitem方法删除字典最末尾属性\n",
    "emp1.popitem()\n",
    "print(emp1, type(emp1))\n",
    "\n",
    "# 使用del方法删除字典的某个属性\n",
    "del emp1[\"salary\"]\n",
    "print(emp1, type(emp1))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'id': 1, 'name': 'Tom', 'gender': 'male', 'salary': 2000, 'extraHours': 8, 'hourlyRate': 30} <class 'dict'>\n",
      "1\n",
      "Tom\n"
     ]
    }
   ],
   "source": [
    "# 从字典中获取某个属性的值\n",
    "emp1 = {\n",
    "    \"id\": 1,\n",
    "    \"name\": \"Tom\",\n",
    "    \"gender\": \"male\",\n",
    "    \"salary\": 2000,\n",
    "    \"extraHours\": 8,\n",
    "    \"hourlyRate\": 30\n",
    "}\n",
    "print(emp1, type(emp1))\n",
    "\n",
    "# 以下标方式获取id\n",
    "print(emp1[\"id\"])\n",
    "\n",
    "# 以get方法获取name\n",
    "print(emp1.get(\"name\"))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'id': 1, 'name': 'Tom', 'gender': 'male', 'salary': 2000, 'extraHours': 8, 'hourlyRate': 30} <class 'dict'>\n",
      "{'id': 1, 'name': 'Jerry', 'gender': 'male', 'salary': 2200, 'extraHours': 8, 'hourlyRate': 30} <class 'dict'>\n",
      "{'id': 1, 'name': 'Cherry', 'gender': 'male', 'salary': 2200, 'extraHours': 8, 'hourlyRate': 30} <class 'dict'>\n"
     ]
    }
   ],
   "source": [
    "# 更新字典中的属性值\n",
    "emp1 = {\n",
    "    \"id\": 1,\n",
    "    \"name\": \"Tom\",\n",
    "    \"gender\": \"male\",\n",
    "    \"salary\": 2000,\n",
    "    \"extraHours\": 8,\n",
    "    \"hourlyRate\": 30\n",
    "}\n",
    "print(emp1, type(emp1))\n",
    "\n",
    "# 直接属性值更新\n",
    "emp1[\"name\"] = \"Jerry\"\n",
    "emp1[\"salary\"] = 2200\n",
    "print(emp1, type(emp1))\n",
    "\n",
    "# 使用update方法更新\n",
    "emp1.update(name=\"Cherry\", salary=2200)\n",
    "print(emp1, type(emp1))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dict_keys(['id', 'name', 'gender', 'salary', 'extraHours', 'hourlyRate']) <class 'dict_keys'>\n"
     ]
    }
   ],
   "source": [
    "# 获取字典的键值\n",
    "d1 = emp1.keys()\n",
    "print(d1, type(d1))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['id', 'name', 'gender', 'salary', 'extraHours', 'hourlyRate'] <class 'list'>\n"
     ]
    }
   ],
   "source": [
    "# 转换字典的键值为列表\n",
    "l1 = list(emp1.keys())\n",
    "\n",
    "print(l1, type(l1))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "id\n",
      "name\n",
      "gender\n",
      "salary\n",
      "extraHours\n",
      "hourlyRate\n"
     ]
    }
   ],
   "source": [
    "# 以字典的keys进行迭代\n",
    "for x in emp1.keys():\n",
    "    print(x)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dict_values([1, 'Cherry', 'male', 2200, 8, 30]) <class 'dict_values'>\n"
     ]
    }
   ],
   "source": [
    "# 获取字典的values\n",
    "v1 = emp1.values()\n",
    "print(v1, type(v1))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 'Cherry', 'male', 2200, 8, 30] <class 'list'>\n"
     ]
    }
   ],
   "source": [
    "# 把字典values转成列表\n",
    "l1 = list(emp1.values())\n",
    "print(l1, type(l1))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "Cherry\n",
      "male\n",
      "2200\n",
      "8\n",
      "30\n"
     ]
    }
   ],
   "source": [
    "# 以字典的值做迭代\n",
    "for x in emp1.values():\n",
    "    print(x)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 集合"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "set() <class 'set'>\n",
      "{1, 'b', 2} <class 'set'>\n",
      "{1, 2, 3, 4, 5} <class 'set'>\n"
     ]
    }
   ],
   "source": [
    "# 集合初始化\n",
    "s = set()\n",
    "\n",
    "print(s, type(s))\n",
    "\n",
    "s.add(1)\n",
    "s.add('b')\n",
    "s.add(2)\n",
    "print(s, type(s))\n",
    "\n",
    "s = {1, 2, 3, 4, 5}\n",
    "print(s, type(s))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1, 2, 3, 4, 5} <class 'set'>\n",
      "{1, 2, 3, 4, 5} <class 'set'>\n"
     ]
    },
    {
     "ename": "KeyError",
     "evalue": "11",
     "output_type": "error",
     "traceback": [
      "\u001B[0;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[0;31mKeyError\u001B[0m                                  Traceback (most recent call last)",
      "\u001B[0;32m/var/folders/mr/zthpdwtn37g2h4t97k6tv1pw0000gn/T/ipykernel_2308/2093376859.py\u001B[0m in \u001B[0;36m<module>\u001B[0;34m\u001B[0m\n\u001B[1;32m      8\u001B[0m \u001B[0;34m\u001B[0m\u001B[0m\n\u001B[1;32m      9\u001B[0m \u001B[0;31m# 在集合中删除指定的元素，删除某个不存在的元素是将报错\u001B[0m\u001B[0;34m\u001B[0m\u001B[0;34m\u001B[0m\u001B[0m\n\u001B[0;32m---> 10\u001B[0;31m \u001B[0ms\u001B[0m\u001B[0;34m.\u001B[0m\u001B[0mremove\u001B[0m\u001B[0;34m(\u001B[0m\u001B[0;36m11\u001B[0m\u001B[0;34m)\u001B[0m\u001B[0;34m\u001B[0m\u001B[0;34m\u001B[0m\u001B[0m\n\u001B[0m\u001B[1;32m     11\u001B[0m \u001B[0mprint\u001B[0m\u001B[0;34m(\u001B[0m\u001B[0ms\u001B[0m\u001B[0;34m,\u001B[0m \u001B[0mtype\u001B[0m\u001B[0;34m(\u001B[0m\u001B[0ms\u001B[0m\u001B[0;34m)\u001B[0m\u001B[0;34m)\u001B[0m\u001B[0;34m\u001B[0m\u001B[0;34m\u001B[0m\u001B[0m\n",
      "\u001B[0;31mKeyError\u001B[0m: 11"
     ]
    }
   ],
   "source": [
    "# 集合元素操作\n",
    "s = {1, 2, 3, 4, 5}\n",
    "print(s, type(s))\n",
    "\n",
    "# 在指定集合中丢弃某个元素，元素不存在时不会报错\n",
    "s.discard(11)\n",
    "print(s, type(s))\n",
    "\n",
    "# 在集合中删除指定的元素，删除某个不存在的元素是将报错\n",
    "s.remove(11)\n",
    "print(s, type(s))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1, 2, 3, 4, 5} <class 'set'>\n",
      "{2, 3, 4, 5} <class 'set'>\n",
      "{2, 4, 5} <class 'set'>\n",
      "{2, 19, 4, 5, 7, 11} <class 'set'>\n",
      "set() <class 'set'>\n"
     ]
    }
   ],
   "source": [
    "# 集合元素的其他操作\n",
    "s = {1, 2, 3, 4, 5}\n",
    "print(s, type(s))\n",
    "\n",
    "# pop操作将删除某个集合元素，操作是无序的\n",
    "s.pop()\n",
    "print(s, type(s))\n",
    "\n",
    "# remove方法，在集合中删除指定的元素\n",
    "s.remove(3)\n",
    "print(s, type(s))\n",
    "\n",
    "# update方法，类似于union操作\n",
    "s.update({5, 7, 11, 19})\n",
    "print(s, type(s))\n",
    "\n",
    "# clear方法，清空集合元素\n",
    "s.clear()\n",
    "print(s, type(s))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原始集合\n",
      "{1, 2, 3, 4, 5} <class 'set'>\n",
      "{3, 4, 5, 6, 7} <class 'set'>\n",
      "交集:\n",
      "{3, 4, 5}\n",
      "差集:\n",
      "{1, 2, 3, 4, 5} - {3, 4, 5, 6, 7} = {1, 2}\n",
      "{3, 4, 5, 6, 7} - {1, 2, 3, 4, 5} = {6, 7}\n",
      "并集:\n",
      "{1, 2, 3, 4, 5} union {3, 4, 5, 6, 7} = {1, 2, 3, 4, 5, 6, 7}\n",
      "是否子集:\n",
      "False\n",
      "是否父集:\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "# 集合运算\n",
    "s1 = {1, 2, 3, 4, 5}\n",
    "s2 = {3, 4, 5, 6, 7}\n",
    "\n",
    "print('原始集合')\n",
    "print(s1, type(s1))\n",
    "print(s2, type(s2))\n",
    "\n",
    "# 交集\n",
    "print('交集:')\n",
    "s = s1.intersection(s2)\n",
    "print(s)\n",
    "\n",
    "# 差集\n",
    "print(\"差集:\")\n",
    "s = s1.difference(s2)\n",
    "print(f\"{s1} - {s2} = {s}\")\n",
    "s = s2.difference(s1)\n",
    "print(f\"{s2} - {s1} = {s}\")\n",
    "\n",
    "# 并集\n",
    "print(\"并集:\")\n",
    "s = s1.union(s2)\n",
    "print(f\"{s1} union {s2} = {s}\")\n",
    "\n",
    "# 是否子集合\n",
    "print(\"是否子集:\")\n",
    "print(s1.issubset(s2))\n",
    "\n",
    "# 是否父集合\n",
    "print(\"是否父集:\")\n",
    "print(s1.issuperset(s2))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 元组\n",
    "\n",
    "略"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 字符串"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello, World <class 'str'>\n",
      "Hello, World <class 'str'>\n",
      "WHellooHellorHellolHellod <class 'str'>\n"
     ]
    }
   ],
   "source": [
    "# 字符串拼接\n",
    "s = \"Hello, World\"\n",
    "print(s, type(s))\n",
    "\n",
    "# 字符串加法\n",
    "s1 = \"Hello, \"\n",
    "s2 = \"World\"\n",
    "s = s1 + s2\n",
    "print(s, type(s))\n",
    "\n",
    "# 字符串连接，join\n",
    "s1 = \"Hello\"\n",
    "s2 = \"World\"\n",
    "s = s1.join(s2)\n",
    "print(s, type(s))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "12\n",
      "l\n",
      "d\n"
     ]
    }
   ],
   "source": [
    "# 字符串统计\n",
    "#    012345678901\n",
    "s = \"Hello, World\"\n",
    "\n",
    "# 测量字符串长度\n",
    "print(len(s))\n",
    "\n",
    "# 字符串作为序列，它的位置是从0开始的，可以用下标访问字符串的某个字符\n",
    "print(s[3])\n",
    "\n",
    "# 下标 -1 代表最后一个位置的字符\n",
    "print(s[-1])"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "klmnopqrst\n",
      "kmoqs\n",
      "klmnopqrstuvwxyz\n",
      "abcdefghij\n",
      "klmnopqrstuvwxy\n",
      "abcdefghijklmnopqrstuvwxyz\n",
      "zyxwvutsrqponmlkjihgfedcba\n"
     ]
    }
   ],
   "source": [
    "# 字符串切片\n",
    "#              1         2\n",
    "#    01234567890123456789012345\n",
    "s = \"abcdefghijklmnopqrstuvwxyz\"\n",
    "print(s[10:20])     # 位置访问，顾头不顾尾\n",
    "print(s[10:20:2])   # 先切片再按步长取\n",
    "print(s[10:])       # 没指定边界时\n",
    "print(s[:10])       # 没指定边界时\n",
    "print(s[10:-1])\n",
    "print(s[:])         # 等价于全选\n",
    "print(s[::-1])      # 等价于反转"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['H', 'e', 'l', 'l', 'o', ',', ' ', 'W', 'o', 'r', 'l', 'd']\n"
     ]
    }
   ],
   "source": [
    "# 字符串转换为列表\n",
    "s = \"Hello, World\"\n",
    "print(list(s))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Python', 'is', 'an', 'easy', 'to', 'learn', 'and', 'powerful', 'programming', 'language'] <class 'list'>\n",
      "['Python is an easy to learn and powerful', 'programming', 'language'] <class 'list'>\n",
      "['1', 'Tom', 'male', '2000'] <class 'list'>\n",
      "['Beautiful is better than ugly.', 'Explicit is better than implicit.', 'Simple is better than complex.', 'Complex is better than complicated.', 'Flat is better than nested.']\n"
     ]
    }
   ],
   "source": [
    "# 字符串split（分裂）操作（缺省操作）\n",
    "s = \"Python is an easy to learn and powerful programming language\"\n",
    "s1 = s.split()\n",
    "print(s1, type(s1))\n",
    "\n",
    "# 从字符串右侧开始分裂，并且只分裂2次\n",
    "s1 = s.rsplit(' ', 2)\n",
    "print(s1, type(s1))\n",
    "\n",
    "# 字符串指定分裂字符\n",
    "s = \"1,Tom,male,2000\"\n",
    "s1 = s.split(',')   # 指定逗号为分裂字符\n",
    "print(s1, type(s1))\n",
    "\n",
    "# 行分裂\n",
    "s = '''Beautiful is better than ugly.\n",
    "Explicit is better than implicit.\n",
    "Simple is better than complex.\n",
    "Complex is better than complicated.\n",
    "Flat is better than nested.\n",
    "'''\n",
    "\n",
    "s1 = s.splitlines()\n",
    "print(s1)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  Python is an easy to learn   <class 'str'>\n",
      "Python is an easy to learn <class 'str'>\n",
      "Python is an == easy to learn <class 'str'>\n",
      "Python is an == easy to learn <class 'str'>\n"
     ]
    }
   ],
   "source": [
    "# 字符串压缩\n",
    "s = \"  Python is an easy to learn  \"\n",
    "print(s, type(s))\n",
    "\n",
    "# 压缩字符串两端的空格\n",
    "s1 = s.strip()\n",
    "print(s1, type(s1))\n",
    "\n",
    "# 压缩两端的等号\n",
    "s = \"==Python is an == easy to learn==\"\n",
    "s1 = s.strip('=')\n",
    "print(s1, type(s1))\n",
    "\n",
    "# 压缩不同的字符\n",
    "s = \"¥¥++Python is an == easy to learn--$$\"\n",
    "s1 = s.strip(\"$¥+-\")\n",
    "print(s1, type(s1))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "31\n",
      "31\n",
      "-1\n"
     ]
    }
   ],
   "source": [
    "# 字符串查找 find\n",
    "s = \"Python is an easy to learn and powerful programming language\"\n",
    "\n",
    "# 返回查找内容在字符串中的位置\n",
    "pst = s.find('powerful')\n",
    "print(pst)\n",
    "\n",
    "# 指定起始位置查找\n",
    "pst = s.find('powerful', 10)\n",
    "print(pst)\n",
    "\n",
    "# 当没有找到时返回-1\n",
    "pst = s.find('Java')\n",
    "print(pst)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "True\n",
      "python is an easy to learn\n",
      "PYTHON IS AN EASY TO LEARN\n",
      "10 + 12 = 22\n"
     ]
    }
   ],
   "source": [
    "# 字符串其他操作\n",
    "s = \"89c\"\n",
    "print(s.endswith('c'))  # 判断字符串是否以指定字符结束\n",
    "\n",
    "s = \"¥100\"\n",
    "print(s.startswith('¥'))  # 判断字符串是否以指定字符开始\n",
    "\n",
    "s = \"Python is an easy to learn\"\n",
    "print(s.lower())    # 把字符串转换为小写\n",
    "print(s.upper())    # 把字符串转换为大写\n",
    "\n",
    "a = 10\n",
    "b = 12\n",
    "print(f\"{a} + {b} = {a + b}\")   # 字符串的格式化输出"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### if 条件语句"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "可以参军\n"
     ]
    }
   ],
   "source": [
    "# 简单if\n",
    "# 修改 age（年龄）和 gender（性别）两个条件测试下面代码\n",
    "age = 19\n",
    "gender = 'male'\n",
    "\n",
    "if age > 18 and gender == 'male':\n",
    "    print('可以参军')\n",
    "else:\n",
    "    print('不可以参军')"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "不可以参军\n"
     ]
    }
   ],
   "source": [
    "# 嵌套if\n",
    "# 修改 age（年龄）和 gender（性别）两个条件测试下面代码\n",
    "age = 70\n",
    "gender = 'male'\n",
    "\n",
    "if age > 18 and gender == 'male':\n",
    "    if age > 65:\n",
    "        print('不可以参军')\n",
    "    else:\n",
    "        print('可以参军')\n",
    "else:\n",
    "    print('不可以参军')"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "天气不错\n"
     ]
    }
   ],
   "source": [
    "# 多个if条件并列\n",
    "temp = 22\n",
    "\n",
    "if temp < 5:\n",
    "    print(\"天气很冷，注意保暖\")\n",
    "elif temp < 15:\n",
    "    print(\"天气有点冷\")\n",
    "elif temp < 25:\n",
    "    print(\"天气不错\")\n",
    "else:\n",
    "    print(\"天气有点热\")"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### for 循环"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "finished\n"
     ]
    }
   ],
   "source": [
    "# 按 [0, 5) 产生的整数序列进行循环\n",
    "for x in range(5):\n",
    "    print(x)\n",
    "else:\n",
    "    print(\"finished\")"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "finished\n"
     ]
    }
   ],
   "source": [
    "# 按 [1, 5) 产生的整数序列进行循环\n",
    "for x in range(1, 5):\n",
    "    print(x)\n",
    "else:\n",
    "    print(\"finished\")"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "3\n",
      "5\n",
      "7\n",
      "9\n",
      "finished\n"
     ]
    }
   ],
   "source": [
    "# 按 [1, 10) 产生的整数序列，以步长 2 进行循环\n",
    "for x in range(1, 10, 2):\n",
    "    print(x)\n",
    "else:\n",
    "    print(\"finished\")"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3.14\n",
      "2.71\n",
      "Hello\n",
      "Age\n",
      "finished\n"
     ]
    }
   ],
   "source": [
    "# 按给定的列表进行循环\n",
    "lst = [3.14, 2.71, 'Hello', 'Age']\n",
    "for x in lst:\n",
    "    print(x)\n",
    "else:\n",
    "    print(\"finished\")"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3.14\n",
      "2.71\n",
      "Hello\n",
      "Age\n",
      "finished\n"
     ]
    }
   ],
   "source": [
    "# 按给定的元组进行循环\n",
    "lst = (3.14, 2.71, 'Hello', 'Age')\n",
    "for x in lst:\n",
    "    print(x)\n",
    "else:\n",
    "    print(\"finished\")"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2.71\n",
      "3.14\n",
      "Hello\n",
      "Age\n",
      "finished\n"
     ]
    }
   ],
   "source": [
    "# 按给定的集合进行循环\n",
    "lst = {3.14, 2.71, 'Hello', 'Age'}\n",
    "for x in lst:\n",
    "    print(x)\n",
    "else:\n",
    "    print(\"finished\")"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### while 循环"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "finished\n"
     ]
    }
   ],
   "source": [
    "# while 循环: for x in range(5)\n",
    "n = 0\n",
    "while n < 5:\n",
    "    print(n)\n",
    "    n += 1\n",
    "else:\n",
    "    print(\"finished\")"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "finished\n"
     ]
    }
   ],
   "source": [
    "# for x in range(1, 5)\n",
    "n = 1\n",
    "while n < 5:\n",
    "    print(n)\n",
    "    n += 1\n",
    "else:\n",
    "    print(\"finished\")"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "3\n",
      "5\n",
      "7\n",
      "9\n",
      "finished\n"
     ]
    }
   ],
   "source": [
    "# for x in range(1, 10, 2)\n",
    "n = 1\n",
    "while n < 10:\n",
    "    print(n)\n",
    "    n += 2\n",
    "else:\n",
    "    print(\"finished\")"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n"
     ]
    }
   ],
   "source": [
    "# break\n",
    "n = 1\n",
    "while True:\n",
    "    print(n)\n",
    "    n += 1\n",
    "    if n > 5:\n",
    "        break\n",
    "else:\n",
    "    print(\"finished\")"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}